package com.mit.service.sale.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.sale.dto.CustomerDTO;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.sale.dao.ICustomerDAO;

public class CustomerDAOImpl extends AbstractDAO implements ICustomerDAO {

	@Override
	public CustomerDTO createCustomerDTO(CustomerDTO to) {
		Integer id = (Integer) getSqlMapClientTemplate().insert("insertCustomer", to);
		to.setId(id);
		return to;
	}

	@Override
	public List<CustomerDTO> retrievePagedCustomers(Integer orgId, String searchKey, Integer pageNo, Integer pageSize) {
		CustomerDTO to = new CustomerDTO();
		to.setName(searchKey);
		to.setPageNo(pageNo);
		to.setPageSize(pageSize);
		to.setOrgId(orgId);
		return getSqlMapClientTemplate().queryForList("selectPagedCustomers", to);
	}

	@Override
	public List<CustomerDTO> retrieveCustomerBasicInfo(Integer orgId, String searchKey) {
		HashMap param = new HashMap();
		param.put("name", searchKey);
		param.put("orgId", orgId);
		return getSqlMapClientTemplate().queryForList("selectCustomerBasicInfo", param);
	}

	@Override
	public Integer retrieveCustomersCount(Integer orgId, String searchKey) {
		CustomerDTO to = new CustomerDTO();
		to.setName(searchKey);	
		to.setOrgId(orgId);
		return (Integer) getSqlMapClientTemplate().queryForObject("selectCustomersCount", to);
	}

	@Override
	public CustomerDTO updateCustomerDTO(CustomerDTO to)
			throws NoRecordUpdateException {
		int rows = getSqlMapClientTemplate().update("updateCustomer",to);
		if(rows < 1)
			throw new NoRecordUpdateException();
		return to;
	}

	@Override
	public boolean hasCustomer(Integer orgId, Integer id, String name) {
		HashMap param = new HashMap();
		param.put("id", id);
		param.put("name", name);
		param.put("orgId", orgId);
		Integer rowCount = (Integer) getSqlMapClientTemplate().queryForObject("selectCustomerByName", param);
		return (rowCount > 0);
	}

}
